<script setup lang='ts'>
import { useRouter } from "vue-router";
import { ref, reactive } from "vue";
import { Edit, Delete, Search } from "@element-plus/icons-vue";
import { memberHook } from "../../hooks/memberHooks";
import memberStore from "../../store/member";
const router = useRouter();

const _memHook = memberHook();
_memHook.getMember(getData);

function getData() {
  let member = memberStore();
  tableData.value = member.getList;
  console.log("table数据", tableData);
}

let tableData = ref<Array<any>>([]);

const formInline = reactive({
  user: "",
  part: "",
});

const currentPage = ref<number>(1);
const pageSize = ref<number>(5);
const total = ref<number>(10);

const pageChange = (val: any): void => {
  currentPage.value = val;
};

const updateStatus = (e: any, status: number) => {
  let id = e.currentTarget.dataset.id;
  console.log("id:", id, "status:", status);
  _memHook.updateStatus(id, status, getData);
};

const toUpdate = (e: any, type: number) => {
  type ? type : 0;
  let data = {};
  if (type === 1) {
    let id = e.currentTarget.dataset.id;
    data = { id, type };
  } else {
    data = { type };
  }
  router.push({ name: "memberUpdate", query: data });
};
</script>
<!-- 模型 -->
<template>
  <div class="">
    <!-- 顶部搜索部分 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="formInline.user" placeholder="用户名/姓名" />
      </el-form-item>
      <el-form-item>
        <el-select v-model="formInline.part" placeholder="选择所属部门">
          <el-option label="销售部" value="1" />
          <el-option label="运营部" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search">查询</el-button>
        <el-button type="primary" plain @click="toUpdate($event, 0)"
          >添加成员</el-button
        >
      </el-form-item>

      <!-- 表格 -->
    </el-form>
    <el-table :data="tableData" center border style="width: 100%">
      <el-table-column type="selection" width="50" />
      <el-table-column prop="id" label="ID" width="50" />
      <el-table-column prop="username" label="账号" width="180" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="updateTime" label="上次登录" />
      <el-table-column prop="status" label="状态">
        <template #default="s">
          <span
            v-if="s.row.status == 0"
            class="status success"
            :data-id="s.row.id"
            @click="updateStatus($event, 1)"
            >启用</span
          >
          <span
            v-else
            class="status danger"
            :data-id="s.row.id"
            @click="updateStatus($event, 0)"
            >禁用</span
          >
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="s">
          <el-button
            type="primary"
            :data-id="s.row.id"
            :icon="Edit"
            @click="toUpdate($event, 1)"
          />
          <el-button type="danger" :data-id="s.row.id" :icon="Delete" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="pageChange"
    />
  </div>
</template>

<style scoped>
::v-deep th div,
::v-deep td div {
  text-align: center;
}

::v-deep .el-pagination {
  margin-top: 8px;
  justify-content: flex-end;
}
</style>